<?php
//后台公共函数库，没有缓存
//生成左侧
function syscolumn($parid){
   $syscolumn=M("syscolumn");
   $admin_user=$_SESSION["admin"];
   $admin=M("admin");
   $pression=$admin->where("admin='$admin_user'")->getField("pression");
   $map["id"]=array("in",$pression);
   $map["parid"]=$parid;
   $map["is_show"]=1;
   $par_col=$syscolumn->where($map)->order("sequence desc")->findAll();
   if(empty($par_col)) return false;
   foreach($par_col as $k=>$v){
      $item=array();
      $item["p"]=$v;
	  $id=$v["id"];
	  $map["parid"]=$id;
	  $child_col=$syscolumn->where($map)->order("sequence desc")->findAll();
	  $item["c"]=$child_col;
	  $col[$k]=$item;
   }
   return $col;
}

//建立栏目
//$col_name 栏目名称
//$col_eng_name 栏目英文名称
function create_column($col_name,$col_eng_name,$charset="utf8"){
   //栏目名称,栏目英文名称,建立数据表,建立action,model, index edit ad
    $syscolumn=D("syscolumn"); 
    if(empty($col_name)) return false;
	if(empty($col_eng_name)) return false;
	//先以$col_eng_name建立数据表 id为第一列  id为主键 
	$sql="create table $col_eng_name (id int auto_increment comment '编号' not null,primary key(id)) engine=innodb character set utf8 collate utf8_general_ci comment='$col_name'";
    $syscolumn->execute($sql);
	CreateModel($col_eng_name);
	CreateAction($col_eng_name);
	CreateTpl($col_eng_name);
}

function update_column($col_name,$old_eng_name,$new_eng_name,$charset="utf8"){
    //修改数据表名称 修改sysfield中的col_eng_name  删除以前的model action tpl
	   if(empty($col_name)) return false;
	   if(empty($old_eng_name)) return false;
	   if(empty($new_eng_name)) return false;
	   $syscolumn=M("syscolumn");
	   $sql="rename table $old_eng_name to $new_eng_name";//修改表名称
	   $syscolumn->execute($sql);
	   $sql="alter table $new_eng_name comment='$col_name'";//修改表注释
	   $syscolumn->execute($sql);
	   $sysfield=M("sysfield");
	   $sysfield->where("col_eng_name='$old_eng_name'")->setField("col_eng_name",$new_eng_name);
	   //删除
	   $model_path=APP_NAME."/Lib/Model/".ucfirst($old_eng_name)."Model.class.php";
	   $tpl_dir=APP_NAME."/Tpl/default/Admin/".ucfirst($old_eng_name);
	   $action_path=APP_NAME."/Lib/Action/Admin/".ucfirst($old_eng_name)."Action.class.php";
	   Vendor("LJW.Inc_File");
	   $myfile=new Inc_File();
	   $myfile->deletefile($model_path);
	   $myfile->deletefile($action_path);
	   $myfile->deletedir($tpl_dir);
	   CreateModel($new_eng_name);
	   CreateAction($new_eng_name);
	   CreateTpl($new_eng_name);
}

function delete_column($col_eng_name){
    //删除数据表  删除sysfield 中col_eng_name  删除model action tpl
	$sysfield=M("sysfield");
	$sql="drop table $col_eng_name";
	$sysfield->execute($sql);
	
	$sysfield->startTrans();
	if($sysfield->where("col_eng_name='$col_eng_name'")->delete()){
	    $sysfield->commit();
	}else{
	    $sysfield->rollback();
	}
	//删除
	$model_path=APP_NAME."/Lib/Model/".ucfirst($col_eng_name)."Model.class.php";
	$tpl_dir=APP_NAME."/Tpl/default/Admin/".ucfirst($col_eng_name);
	$action_path=APP_NAME."/Lib/Action/Admin/".ucfirst($col_eng_name)."Action.class.php";
	Vendor("LJW.Inc_File");
	$myfile=new Inc_File();
	$myfile->deletefile($model_path);
	$myfile->deletefile($action_path);
	$myfile->deletedir($tpl_dir);
}
function CreateModel($col_eng_name){
    $sys_path="system/";
	$model_path=APP_NAME."/Lib/Model/";
	chmod($sys_path,0777);
	chmod($model_path,0777);
	$model_content=file_get_contents($sys_path."TemplateModel.class.php");
	$model_content=str_replace("<Template>",ucfirst($col_eng_name),$model_content);
	$model_filename=$model_path.ucfirst($col_eng_name)."Model.class.php";
	if($fp=fopen($model_filename,"wb")){
	     flock($fp,LOCK_EX);
		 fwrite($fp,$model_content);
		 flock($fp,LOCK_UN);
		 fclose($fp);
	}
}
function CreateAction($col_eng_name){
    $sys_path="system/";
	$action_path=APP_NAME."/Lib/Action/Admin/";
	chmod($sys_path,0777);
	chmod($action_path,0777);
	$action_content=file_get_contents($sys_path."TemplateAction.class.php");
	$action_content=str_replace("<Template>",ucfirst($col_eng_name),$action_content);
	$action_filename=$action_path.ucfirst($col_eng_name)."Action.class.php";
    if($fp=fopen($action_filename,"wb")){
	     flock($fp,LOCK_EX);
		 fwrite($fp,$action_content);
		 flock($fp,LOCK_UN);
		 fclose($fp);
	}
}
function CreateTpl($col_eng_name){
    Vendor("LJW.Inc_File");
	$myfile=new Inc_File();
	$sys_path="system/Template";
	$tpl_path=APP_NAME."/Tpl/default/Admin/".ucfirst($col_eng_name);
	chmod($tpl_path,0777);
	chmod($sys_path,0777);
	$myfile->copydir($sys_path,$tpl_path,true);
}
//$col_arr 栏目字段数组 字段名称(field_name)  字段类型(field_type)  字段长度(field_length) 字段描述(field_comment)  输出框(field_output_type) 输出框长度(field_output_length)  说明文字(notice) 字段值填充类型(field_value_fill_type)  字段值填充(field_value_fill)   字段默认值(field_default_value) 起用状态(field_isuse) 详细页(field_isdetail)  列表页(field_islist)
function col_arr__2_engname($engname){
     if(empty($engname)) return false;
	 $syscolumn=D("syscolumn");
	 $arr=$syscolumn->query("show full columns from $engname");
	 return $arr;
}
//生成表单项
//$fieldname 字段名称
//$table 表名称
//$type 类型  "add"  "edit" 
//$pk_value
function  showformitembyfield($fieldname,$table,$type="add",$pk_value=null){
    if(empty($fieldname)) return false;
	if(empty($table)) return false;

    $syscolumn=M("syscolumn");
	$col_id=$syscolumn->where("col_eng_name='$table'")->getField("id");
	
	$sysfield=M("sysfield");
	$model=M($table);
	$field_arr=$sysfield->where("col_eng_name='$table' and field_name='$fieldname'")->find();
     
	//输出框类型
	$output_type=$field_arr["field_output_type"];
	//注意事项
	$field_notice=$field_arr["field_notice"];
	if(!empty($field_notice)) $notice="&nbsp;&nbsp;<span style='color:red'>$field_notice</span>";
	//值
	if($type=="add"){
	    $value=$field_arr["field_default_value"];
	}elseif($type=="edit"){
		$pk=$model->getPk();
		$value=$model->where("$pk=$pk_value")->getField($fieldname);
	}
	//长度
	$size=$field_arr["field_output_length"];
	//说明文字
	$comment=$field_arr["field_comment"];
	//填充类型
	$fill_type=$field_arr["field_value_fill_type"];
	//填充
	$fill_value=$field_arr["field_value_fill"];
    
	switch($output_type){
	     case "单行文本框":
		     echo <<<EOT
			 <tr><td>$comment</td><td><input name="$fieldname" id="$fieldname" type="text" value="$value"  size="$size"></td><td>$notice</td></tr>
EOT;
         break;
         case "密码框":
		      echo <<<EOT
			 <tr><td>$comment</td><td><input name="$fieldname" id="$fieldname" type="password" value="$value"  size="$size"></td><td>$notice</td></tr>
EOT;
         break;
		 case "图片上传":
		     $small_id="small_pic_".$fieldname;
			 $is_thumb=$field_arr["is_thumb"]?$field_arr["is_thumb"]:0;//是否要生成缩略图
			 $thumb_w=$field_arr["thumb_w"]?$field_arr["thumb_w"]:0;//宽度
			 $thumb_h=$field_arr["thumb_h"]?$field_arr["thumb_h"]:0;//高度
			 $thumb_label=$field_arr["thumb_label"]?$field_arr["thumb_label"]:0;//标志
			 $thumb_type=$field_arr["thumb_type"]?$field_arr["thumb_type"]:0;//类型
			 $is_wm=$field_arr["is_wm"]?$field_arr["is_wm"]:0;//是否要打水印
			 $wm_type=$field_arr["wm_type"]?$field_arr["wm_type"]:0;//水印类型
			 $wm_pos=$field_arr["wm_pos"]?$field_arr["wm_pos"]:0;//水印位置
			 //如果没有图片，显示默认图片
			 $thumb_value=empty($value)?"Public/images/admin/no.jpg":$value;

		     echo <<<EOT
			 <tr><td>缩略图</td><td><a href="__ROOT__/$value" target="_blank"><img src="__ROOT__/$thumb_value" id="$small_id" name="$small_id" width="50" height="50" border="0" title="点击查看大图"/></a></td><td></td></tr>
		     <tr><td>$comment</td><td><input type="text" id="$fieldname" name="$fieldname" value="$value" size="$size"/><input type="button" name="upload" id="upload" value="上传" onClick="var picpath=window.showModalDialog('__APP__/Admin/Public/upload/is_thumb/$is_thumb/thumb_w/$thumb_w/thumb_h/$thumb_h/thumb_label/$thumb_label/thumb_type/$thumb_type/is_wm/$is_wm/wm_type/$wm_type/wm_pos/$wm_pos','','dialogWidth=400px;dialogHeight=180px');if(picpath!==undefined){document.getElementById('$fieldname').value=picpath;document.getElementById('$small_id').src='__ROOT__/'+picpath;}"/>
<input id="btn_select" name="btn_select" value="选择" type="button" onclick="window.open('__APP__/Admin/Public/selectupload/inputid/$fieldname/thumbid/$small_id','选择文件','width=800,height=800,resizable=yes,scrollbars=yes,top=100,left=100');"/></td><td>$notice</td></tr>
EOT;
         break;
		 case "多图片上传":
			 $is_thumb=$field_arr["is_thumb"]?$field_arr["is_thumb"]:0;//是否要生成缩略图
			 $thumb_w=$field_arr["thumb_w"]?$field_arr["thumb_w"]:0;//宽度
			 $thumb_h=$field_arr["thumb_h"]?$field_arr["thumb_h"]:0;//高度
			 $thumb_label=$field_arr["thumb_label"]?$field_arr["thumb_label"]:0;//标志
			 $thumb_type=$field_arr["thumb_type"]?$field_arr["thumb_type"]:0;//类型
			 $is_wm=$field_arr["is_wm"]?$field_arr["is_wm"]:0;//是否要打水印
			 $wm_type=$field_arr["wm_type"]?$field_arr["wm_type"]:0;//水印类型
			 $wm_pos=$field_arr["wm_pos"]?$field_arr["wm_pos"]:0;//水印位置
			 $v=base64_encode($value);
		     echo <<<EOT
		     <tr><td>$comment</td><td><input type="text" id="$fieldname" name="$fieldname" value="$value" size="$size"/><input type="button" name="upload" id="upload" value="上传" onClick="var multpicpath=window.showModalDialog('__APP__/Admin/Public/multiupload/is_thumb/$is_thumb/thumb_w/$thumb_w/thumb_h/$thumb_h/thumb_label/$thumb_label/thumb_type/$thumb_type/is_wm/$is_wm/wm_type/$wm_type/wm_pos/$wm_pos/v/$v','','dialogWidth=800px;dialogHeight=500px');if(multpicpath!==undefined){document.getElementById('$fieldname').value=multpicpath;}"/></td><td>$notice</td></tr>
EOT;
		 break;
		 case "flash多图片上传":
		     $is_thumb=$field_arr["is_thumb"]?$field_arr["is_thumb"]:0;//是否要生成缩略图
			 $thumb_w=$field_arr["thumb_w"]?$field_arr["thumb_w"]:0;//宽度
			 $thumb_h=$field_arr["thumb_h"]?$field_arr["thumb_h"]:0;//高度
			 $thumb_label=$field_arr["thumb_label"]?$field_arr["thumb_label"]:0;//标志
			 $thumb_type=$field_arr["thumb_type"]?$field_arr["thumb_type"]:0;//类型
			 $is_wm=$field_arr["is_wm"]?$field_arr["is_wm"]:0;//是否要打水印
			 $wm_type=$field_arr["wm_type"]?$field_arr["wm_type"]:0;//水印类型
			 $wm_pos=$field_arr["wm_pos"]?$field_arr["wm_pos"]:0;//水印位置
			 $v=base64_encode($value);
		     echo <<<EOT
		     <tr><td>$comment</td><td><input type="text" id="$fieldname" name="$fieldname" value="$value" size="$size"/><input type="button" name="upload" id="upload" value="上传" onClick="document.getElementById('$fieldname').value=window.showModalDialog('__APP__/Admin/Public/flashupload/is_thumb/$is_thumb/thumb_w/$thumb_w/thumb_h/$thumb_h/thumb_label/$thumb_label/thumb_type/$thumb_type/is_wm/$is_wm/wm_type/$wm_type/wm_pos/$wm_pos/v/$v','','dialogWidth=600px;');"/></td><td>$notice</td></tr>
EOT;
		 break;
		 case "文件上传":
		     $is_thumb=$field_arr["is_thumb"]?$field_arr["is_thumb"]:0;//是否要生成缩略图
			 $thumb_w=$field_arr["thumb_w"]?$field_arr["thumb_w"]:0;//宽度
			 $thumb_h=$field_arr["thumb_h"]?$field_arr["thumb_h"]:0;//高度
			 $thumb_label=$field_arr["thumb_label"]?$field_arr["thumb_label"]:0;//标志
			 $thumb_type=$field_arr["thumb_type"]?$field_arr["thumb_type"]:0;//类型
			 $is_wm=$field_arr["is_wm"]?$field_arr["is_wm"]:0;//是否要打水印
			 $wm_type=$field_arr["wm_type"]?$field_arr["wm_type"]:0;//水印类型
			 $wm_pos=$field_arr["wm_pos"]?$field_arr["wm_pos"]:0;//水印位置
		     echo <<<EOT
		     <tr><td>$comment</td><td><input type="text" id="$fieldname" name="$fieldname" value="$value" size="$size"/><input type="button" name="upload" id="upload" value="上传" onClick="document.getElementById('$fieldname').value=window.showModalDialog('__APP__/Admin/Public/upload/is_thumb/$is_thumb/thumb_w/$thumb_w/thumb_h/$thumb_h/thumb_label/$thumb_label/thumb_type/$thumb_type/is_wm/$is_wm/wm_type/$wm_type/wm_pos/$wm_pos','','dialogWidth=400px;dialogHeight=180px');"/><input id="btn_select" name="btn_select" value="选择" type="button" onclick="window.open('__APP__/Admin/Public/selectupload/inputid/$fieldname','选择文件','width=800,height=800,resizable=yes,scrollbars=yes,top=100,left=100');"/></td><td>$notice</td></tr>
EOT;
        break;
        case "单选框":
		     if($fill_type=='枚举类型'){
			      $arr=explode("@",$fill_value);
				  echo "<tr><td>$comment</td><td>";
				  foreach($arr as $k=>$v){
				      $ar=explode("|||",$v);
					  if($ar[0]==$value){
						     echo <<<EOT
							 <input type="radio" name="$fieldname" value="$ar[0]" checked>&nbsp;$ar[1]
EOT;
					  }else{
						      echo <<<EOT
							 <input type="radio" name="$fieldname" value="$ar[0]" >&nbsp;$ar[1]
EOT;
					  }
				  }
				  echo "</td><td>$notice</td></tr>";
			 }elseif($fill_type=='数据表字段填充'){
			      $arr=explode("@",$fill_value);
				  $m=M($arr[0]);
				  $m_farr=$m->getDbFields();
				  if(false!==array_search("sequence",$m_farr,true)){
				     $radio_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->order("sequence desc")->findAll();
				  }else{
				     $radio_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->findAll();
				  }
				  echo "<tr><td>$comment</td><td>";
				  $k_1=$arr[1];
				  $k_2=$arr[2];
				  foreach($radio_arr as $k=>$v){
				       if($v[$k_1]==$value){
				          echo <<<EOT
							   <input type="radio" name="$fieldname" value="$v[$k_1]" checked>&nbsp;$v[$k_2]
EOT;
                      }else{
                          echo <<<EOT
							 <input type="radio" name="$fieldname" value="$v[$k_1]">&nbsp;$v[$k_2]
EOT;
                       }
				  }
				  echo "</td><td>$notice</td></tr>";
			 }
	   break;
	   
	   case "复选框":
	         $fieldname=$fieldname."[]";
			 $value_arr=explode(",",$value);
		     if($fill_type=='枚举类型'){
			      $arr=explode("@",$fill_value);
				  echo "<tr><td>$comment</td><td>";
				  foreach($arr as $k=>$v){
				      $ar=explode("|||",$v);
					     if(false!==array_search($ar[0],$value_arr,true)){
						     echo <<<EOT
							 <input type="checkbox" name="$fieldname" value="$ar[0]" checked>&nbsp;$ar[1]
EOT;
						 }else{
						      echo <<<EOT
							 <input type="checkbox" name="$fieldname" value="$ar[0]" >&nbsp;$ar[1]
EOT;
						 }
				  }
				  echo "</td><td>$notice</td></tr>";
			 }elseif($fill_type=='数据表字段填充'){
			      $arr=explode("@",$fill_value);
				  $m=M($arr[0]);
				  $m_farr=$m->getDbFields();
				  if(false!==array_search("sequence",$m_farr,true)){
				     $radio_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->order("sequence desc")->findAll();
				  }else{
				     $radio_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->findAll();
				  }
				  echo "<tr><td>$comment</td><td><ul style='padding:0px;margin:0px;list-style-type:none'>";
				  foreach($radio_arr as $k=>$v){
				       $radio_value=$arr[1];
					   $radio_key=$arr[2];
				       if(false!==array_search($v[$radio_value],$value_arr,true)){
						  echo "<li style='width:100px;float:left'><input type='checkbox' name=".$fieldname." value=".$v[$radio_value]." checked>&nbsp;".$v[$radio_key]."</li>";
                       }else{
					      echo "<li style='width:100px;float:left'><input type='checkbox' name=".$fieldname." value=".$v[$radio_value]." >&nbsp;".$v[$radio_key]."</li>";
                       }
				  }
				  echo "</ul></td><td>$notice</td></tr>";
			 }
	   break;
	   
	   case "下拉框":
	         if($fill_type=='枚举类型'){
			      $arr=explode("@",$fill_value);
				  echo "<tr><td>$comment</td><td><select name='$fieldname' id='$fieldname'><option value='0'>---请选择---</option>";
				  foreach($arr as $k=>$v){
				      $ar=explode("|||",$v);
					     if($ar[0]==$value){
						     echo <<<EOT
							 <option  value="$ar[0]" selected="selected">$ar[1]</option>
EOT;
						 }else{
						      echo <<<EOT
							 <option  value="$ar[0]" >$ar[1]</option>
EOT;
						 }
				  }
				  echo "</select></td><td>$notice</td></tr>";
			 }elseif($fill_type=='数据表字段填充'){
			      $arr=explode("@",$fill_value);
				  $m=M($arr[0]);
				  $m_farr=$m->getDbFields();
				  echo "<tr><td>$comment</td><td><select name='$fieldname' id='$fieldname'><option value='0'>---请选择---</option>";
				  if(false!==array_search("parid",$m_farr,true)){
				       if($arr[0]=="sysclass"){//如果是系统类别表
					      if(empty($arr[3])){
					        $condition="col_id=$col_id";
						  }else{
						    $condition="$arr[3]";
						  }
						  $item_arr=create_cascade_item($arr[0],0,$arr[2],$condition);
					   }else{
					      if(!empty($arr[3])){
						    $condition=$arr[3];
						  }else{
						    $condition="";
						  }
					      $item_arr=create_cascade_item($arr[0],0,$arr[2],$condition);
					   }
					   $option_str=show_cascade_item($item_arr,$value);
					   echo $option_str;
				  }else{
				      if(false!==array_search("sequence",$m_farr,true)){
				         $option_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->order("sequence desc")->findAll();
				      }else{
				         $option_arr=$m->field("$arr[1],$arr[2]")->where($arr[3])->findAll();
				      }
					  $k_1=$arr[1];
					  $k_2=$arr[2];
					  foreach($option_arr as $k=>$v){
				         if($v[$k_1]==$value){
				             echo <<<EOT
							     <option  value="$v[$k_1]" selected="selected">$v[$k_2]</option>
EOT;
                         }else{
                             echo <<<EOT
							     <option  value="$v[$k_1]">$v[$k_2]</option>
EOT;
                        }
				     }
			     }
				  echo "</td><td>$notice</td></tr>";
			 }
	    break;
	    case  "多行文本框":
		     echo <<<EOT
			 <tr><td>$comment</td><td><textarea name="$fieldname" id="$fieldname" cols="$size" rows="6">$value</textarea></td><td>$notice</td></tr>
EOT;
		break;
		case "编辑框":
		     echo <<<EOT
			 <tr><td colspan='3' align='center'>$comment  $notice</td></tr>
			 <tr><td colspan='3' align='center'>
		     <textarea name="$fieldname" id="$fieldname" style="display:none">$value</textarea><IFRAME ID="eWebEditor1" SRC="__PUBLIC__/Js/ewebeditor/ewebeditor.htm?id=$fieldname&style=gray" FRAMEBORDER="0" SCROLLING="no" WIDTH="550" HEIGHT="350"></IFRAME></td></tr>
EOT;
		break;
		case "兼容firefox编辑框":
		     echo <<<EOT
			 <tr><td colspan='3' align='center'>$comment  $notice</td></tr>
             <tr><td colspan='3' align='center'>
		     <textarea name="$fieldname" id="$fieldname" style="display:none">$value</textarea>
			 <script type="text/javascript" charset="utf-8" src="__PUBLIC__/Js/editor/kindeditor.js"></script>
             <script type="text/javascript">
             KE.show({
               id : "$fieldname",
               cssPath : "__PUBLIC__/Js/editor/index.css",
			   width:620,
			   height:300,
			   resizeMode:1
             });
             </script>
			 </td></tr>
EOT;
		break;
		case "fckeditor编辑框":
	       require_once("Public/Js/fckeditor/fckeditor_php5.php");
           $oFCKeditor = new FCKeditor("$fieldname");
           $oFCKeditor->BasePath= WEB_PUBLIC_PATH."/Js/fckeditor/";
           $oFCKeditor->Value =$value;
           $oFCKeditor->Width=600;
           $oFCKeditor->Height=300;
           $oFCKeditor->ToolbarSet="MyDesign";
           $fckhtml=$oFCKeditor->CreateHtml();
           echo "<tr><td colspan='3' align='center'>$comment  $notice</td></tr><tr><td colspan='3' align='center'>$fckhtml</td></tr>"; 	
	    break;
		case "日期框":
           $default_value=date("Y-m-d");
		   if(empty($value)) $value=$default_value;
		   $calendar_path=WEB_PUBLIC_PATH."/Js/My97DatePicker/WdatePicker.js";
		   echo <<<EOT
		    <script language="javascript" src="$calendar_path"></script>
			<tr><td>$comment</td><td><input name="$fieldname" id="$fieldname" type="text" value="$value"  size="$size" readonly onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" ></td><td>$notice</td></tr>
EOT;
		break;
		case "日期时间框":
           $default_value=date("Y-m-d H:i:s");
		   if(empty($value)) $value=$default_value;
		   $calendar_path=WEB_PUBLIC_PATH."/Js/My97DatePicker/WdatePicker.js";
		   echo <<<EOT
			<script language="javascript" src="$calendar_path"></script>
			<tr><td>$comment</td><td><input name="$fieldname" id="$fieldname" type="text" value="$value"  size="$size" readonly onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"></td><td>$notice</td></tr>
EOT;
		break;
		case "无输出框":
		   echo "";
		break;
		case "仅输出文字":
		   echo <<<EOT
			<tr><td>$comment</td><td>$value</td><td>$notice</td></tr>
EOT;
		   break;
		case "二级下拉联动":
          //例如  用户组  用户
          //用户组表  user_group (group_id group_name)
          //用户表    user(user_id  user_name  user_pwd  g_id)
          
          //填充值  一级表名@值@文本@其他条件|||二级表名@值@文本@其他条件|||$c_field
          //填充如下:  user_group@group_id@group_name|||user@user_id@user_name|||g_id
          if(!empty($fill_value)){
              $fill_value_arr=explode("|||",$fill_value);
              $parent_fill_arr=explode("@",$fill_value_arr[0]);
              $child_fill_arr=explode("@",$fill_value_arr[1]);
              $c_field=$fill_value_arr[2];//二级外键
            
              if(!empty($value)){
                 $child_model=M($child_fill_arr[0]);
                 $child_pk=$child_model->getPk();
                 $p_value=$child_model->where("$child_pk=$value")->getField($c_field);
              }
              
              echo "<tr><td>$comment</td><td>";
                //一级开始
                $p_select="<select  onchange=showchild(this.value,this)><option value='0'>---请选择---</option>";
                if($parent_fill_arr[0]=="sysclass"){//系统类别
                   $item_arr=create_cascade_item($parent_fill_arr[0],0,$parent_fill_arr[2],$parent_fill_arr[3]);
                   $p_options=show_cascade_item($item_arr,$p_value);
                }else{
                   $m=M($parent_fill_arr[0]);
                   $m_farr=$m->getDbFields();
                   if(false!==array_search("sequence",$m_farr,true)){
				         $p_option_arr=$m->field("$parent_fill_arr[1],$parent_fill_arr[2]")->where($parent_fill_arr[3])->order("sequence desc")->findAll();
				   }else{
				         $p_option_arr=$m->field("$parent_fill_arr[1],$parent_fill_arr[2]")->where($parent_fill_arr[3])->findAll();
				   }
				   $k_1=$parent_fill_arr[1];
				   $k_2=$parent_fill_arr[2];
				   foreach($p_option_arr as $k=>$v){
				      if($v[$k_1]==$p_value){
					      $p_options.="<option  value='$v[$k_1]' selected='selected'>$v[$k_2]</option>";
                      }else{
						  $p_options.="<option  value='$v[$k_1]'>$v[$k_2]</option>";
                      }
				   }
                }
                $p_select.=$p_options."</select>";
                //一级结束
                
                //二级开始
                $c_select="<select id='$fieldname' name='$fieldname'><option value='0'>---请选择上一级---</option>";
                if(!empty($value)){
                   if($child_fill_arr[0]=="sysclass"){//系统类别
                     $item_arr=create_cascade_item($child_fill_arr[0],0,$child_fill_arr[2],$child_fill_arr[3]);
                     $c_options=show_cascade_item($item_arr,$value);
                   }else{
                     $m=M($child_fill_arr[0]);
                     $m_farr=$m->getDbFields();
                     if(false!==array_search("sequence",$m_farr,true)){
				         $c_option_arr=$m->field("$child_fill_arr[1],$child_fill_arr[2]")->where($child_fill_arr[3])->order("sequence desc")->findAll();
				     }else{
				         $c_option_arr=$m->field("$child_fill_arr[1],$child_fill_arr[2]")->where($child_fill_arr[3])->findAll();
				     }
				     $k_1=$child_fill_arr[1];
				     $k_2=$child_fill_arr[2];
				     foreach($c_option_arr as $k=>$v){
				       if($v[$k_1]==$value){
					      $c_options.="<option  value='$v[$k_1]' selected='selected'>$v[$k_2]</option>";
                       }else{
						  $c_options.="<option  value='$v[$k_1]'>$v[$k_2]</option>";
                       }
				     } 
                   }
                }else{
                   $c_options="";
                }
                $c_select.=$c_options."</select>";
                //二级结束
                echo $p_select.$c_select."</td><td>$notice</td></tr>";
          }
          $url_str="Admin-Public/ajax2dropdown?c_field=$c_field&pmodelname=$parent_fill_arr[0]&cmodelname=$child_fill_arr[0]&field1=$child_fill_arr[1]&field2=$child_fill_arr[2]";
          $url=U($url_str);
          echo <<<EOT
            <!--确保有jquery.js-->
            <script>
		    function showchild(p_value,obj){
				var html="<option value='0'>---请选择上一级---</option>";
				$.ajax({
				   type:"GET",
				   cache:false,
				   async:false,
				   dataType:"json",
				   data:"p_value="+p_value,
				   url:"$url",
				   success:function(data){
					 data=eval(data);
					 if(data!=undefined){
					  $.each(data,function(i,n){
						 html+="<option value='"+n['$child_fill_arr[1]']+"'>"+n['$child_fill_arr[2]']+"</option>";
					  });
					 }
					 $(obj).next("select").eq(0).html(html);
				   },
				   error:function(){
					  alert('出现错误');   
				   }
				});
			}
		    </script>
EOT;
		break;
        case "左下拉右文本框":
           //填充值:  表名@值字段@文本字段@条件
           //例如    product@id@pr_name@条件
           if(!empty($fill_value)){
              $left_fill_arr=explode("@",$fill_value);
              
              $m=M($left_fill_arr[0]);
              if(!empty($left_fill_arr[3])){
                 if(!empty($value)) $left_value=$m->where("$left_fill_arr[2]='$value' and $left_fill_arr[3]")->getField($left_fill_arr[1]);
              }else{
                  if(!empty($value)) $left_value=$m->where("$left_fill_arr[2]='$value'")->getField($left_fill_arr[1]);
              }
              if($left_fill_arr[0]=="sysclass"){//系统类别表
                  $item_arr=create_cascade_item($left_fill_arr[0],0,$left_fill_arr[2],$left_fill_arr[3]);
                  $options=show_cascade_item($item_arr,$left_value);
              }else{
                  $m_farr=$m->getDbFields();
                  if(false!==array_search("sequence",$m_farr,true)){
				      $option_arr=$m->field("$left_fill_arr[1],$left_fill_arr[2]")->where($left_fill_arr[3])->order("sequence desc")->findAll();
				  }else{
				     $option_arr=$m->field("$left_fill_arr[1],$left_fill_arr[2]")->where($left_fill_arr[3])->findAll();
				  }
				  $k_1=$left_fill_arr[1];
				  $k_2=$left_fill_arr[2];
				  foreach($option_arr as $k=>$v){
				     if($v[$k_1]==$left_value){
					      $options.="<option  value='$v[$k_1]' selected='selected'>$v[$k_2]</option>";
                     }else{
						  $options.="<option  value='$v[$k_1]'>$v[$k_2]</option>";
                     }
				  } 
               }
               $left_select="<select onchange=fill_left(this.value,'$fieldname')><option value='0'>--请选择--</option>".$options."</select>";
               $right_input="<input name='$fieldname' id='$fieldname' type='text' value='$value'  size='$size'>";
              $left_select_right_input=$left_select.$right_input;
              $url=U('Public/ajax2leftselectrightinput?fill_value='.urlencode(base64_encode(serialize($left_fill_arr))));
              echo <<<EOT
			 <tr><td>$comment</td><td>$left_select_right_input</td><td>$notice</td></tr>
             <!--确认有jquery.js-->
             <script>
			 function fill_left(select_value,input_id){
				 if(isNaN(select_value)) return false;
				 $.ajax({
					cache:false,
					async:false,
					type:"GET",
					dataType:"text",
					data:"select_value="+select_value,
					url:"$url",
					success:function(msg){
						$("#"+input_id).val(msg);
					},
					error:function(){
					  alert('出现错误');
					}
				 });
			 }
			 </script>
EOT;
               
           } 
        break;
        case "带颜色选择框的文本框":
          //查看public/js/颜色选择器
          //思路
         $nothtml_value=strip_tags($value);
         $select_color_js=WEB_PUBLIC_PATH."/Js/colorselect.js";
         echo <<<EOT
			 <tr><td>$comment</td><td><input name="$fieldname" id="$fieldname" type="text" value="$nothtml_value"  size="$size"></td><td>$notice</td></tr>
        <script src="$select_color_js"></script><!--需要jquery.js-->
         <script>
         $(function(){
            var style=$("$value").attr("style");
            $("#"+"$fieldname").attr("style",style);
            $("#"+"$fieldname").val("$nothtml_value");
            $("#form2").bind("submit",function(){
                style=$("#"+"$fieldname").attr("style");
                html=$("#"+"$fieldname").val();
                var newhtml="<span style='"+style+"'>"+html+"</span>";
                $("#"+"$fieldname").val(newhtml);
            });
			$("#"+"$fieldname").selectColor();
         });
		 </script> 
EOT;
        break;
		default:
	       echo "";
	}
}
//检测要执行的sql语句,检测成功则执行，否则不执行
function  check_and_execute_sql($sql){
   static $flag=1;
   $model=new Model();
   $model->startTrans();
   if(strpos($sql,"|||")){
       $sql_arr=explode("|||",$sql);
	   foreach($sql_arr as $v){
	      check_and_execute_sql($v);
	   }  
   }else{
       if(strpos($sql,"select")){
	      if(!is_array($model->query($sql))) $flag=0;
	   }else{
	      if(!is_numeric($model->execute($sql))) $flag=0;
	   }
   }
   if(empty($flag)) {
      $model->rollback();
	  return false;
   }else{
      $model->commit();
	  return true;
   }
}

function check_pression(){
    if(empty($_SESSION["admin"])) redirect(U("Login/index"));
	$admin_user=$_SESSION["admin"];
    $admin=M("admin");
	//获取权限
	$pression=$admin->where("admin='$admin_user'")->getField("pression");
	$pression_arr=explode(",",$pression);

    $app_url=__APP__;
    $cur_url=__SELF__;
	$cur_url=str_replace($app_url,"__APP__",$cur_url);
	$syscolumn=M("syscolumn");
	$col_id=$syscolumn->where("col_url='$cur_url'")->getField("id");
    if(!empty($col_id)&&!in_array($col_id,$pression_arr)){
	   alert('你没有操作此栏目的权限，请联系管理员');
	}	
}
?>